package fr.expdev.bndd.widget;

import android.content.Context;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup;
import fr.expdev.bndd.ActivityRefreshable;
import fr.expdev.bndd.R;
import fr.expdev.bndd.data.Post;
import fr.expdev.bndd.data.sqlite.PostsSQLite;
import greendroid.widget.item.TextItem;
import greendroid.widget.itemview.ItemView;

public class PostItem extends TextItem {
	
	private int typeResource;
	
	private int favoriteResource;
	
	private OnClickListener favoriteListener;
	
	public PostItem(final ActivityRefreshable refreshableActivity, final Post post) {
		super(post.getTitle());
		this.typeResource = post.getType().getResource();
		this.favoriteResource = post.isFavorite() ? R.drawable.favorite_on : R.drawable.favorite_off;
		favoriteListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				post.setFavorite(!post.isFavorite());
				PostsSQLite db = new PostsSQLite((Context)refreshableActivity);
				db.open(true);
				db.update(post);
				db.close();
				refreshableActivity.refreshAdapter();
			}
		};
	}

	@Override
	public ItemView newView(Context context, ViewGroup parent) {
		return createCellFromXml(context, R.layout.post_item_view, parent);
	}

	public int getTypeResource() {
		return typeResource;
	}

	public void setTypeResource(int typeResource) {
		this.typeResource = typeResource;
	}

	public int getFavoriteResource() {
		return favoriteResource;
	}

	public void setFavoriteResource(int favoriteResource) {
		this.favoriteResource = favoriteResource;
	}

	public OnClickListener getFavoriteListener() {
		return favoriteListener;
	}

}
